package xx.pro.threadpool.scheduled;

import java.time.LocalDateTime;
import java.util.Random;
import java.util.concurrent.*;

public class ScheduledThreadPool {

    /**
     * 核心线程数2 最大线程数 3
     * @param args
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void main(String[] args)  {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 1,
                TimeUnit.SECONDS, new LinkedBlockingDeque<>(1), new ThreadPoolExecutor.AbortPolicy());
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            threadPoolExecutor.execute(()->{
                System.out.println("task is " + finalI + " 线程：" + Thread.currentThread().getName());
            });
        }
    }


    /**
     * 定时任务单线程
     */
    public void singleSchedu() {
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        System.out.println("添加任务的时间:" + LocalDateTime.now());
        service.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "执行时间：" + LocalDateTime.now());
            }
        }, 2, TimeUnit.SECONDS);
    }

}
